"use strict";
const querystring = require('querystring');
/**
 * @name setList
 * 设置role中的列表数据
 */


//阿里云实名认证接口
const url = 'https://idenauthen.market.alicloudapi.com/idenAuthentication';
const appCode = '8e76ee6fe69344a0887f62b043bcfa42';

/**
 * @name bindIDCard
 * @param string uid 角色ID
 * @param string idCard 身份证ID
 * @param string realName 真实姓名
 * 绑定身份证号码
 */
exports.bindIDCard = function () {
    let uid = this.get('uid');
    let idCard = this.get('idCard');
    let realName = this.get('realName');
    if (!uid || !idCard || !realName) {
        return this.error('uid empty');
    }
    let headers = {
        'Authorization': "APPCODE " + appCode,
        'Content-type': "application/x-www-form-urlencoded;charset=utf-8",
    }
    let body = {
        'idNo': idCard,
        'name': realName,
    }
    return this.library("request/post", url, querystring.stringify(body), true, headers).then(async ret => {
        //判断是否验证成功，成功则写入数据库
        if (ret['respCode'] == '0000') {
            let model = this.model.mongo('role', this.sid);
            await model.set(uid, "idCard", idCard);
            await model.set(uid, "realName", realName);
            var adult = 0;
            //ret['age']为年龄,小于8岁adult设置为1，大于8岁小于16位置为2，16到18为3，大于18为4
            if (ret['age'] < 8) {
                adult = 1;
            } else if (ret['age'] >= 8 && ret['age'] < 12) {
                adult = 2;
            } else if (ret['age'] >= 12 && ret['age'] < 16) {
                adult = 3;
            }else if (ret['age'] >= 16 && ret['age'] < 18) {
                adult = 4;
            } else {
                adult = 5;
            }
            await model.set(uid, "adult", adult);

            // if (ret['age'] >= 18) {
            //     await model.set(uid, "adult", 1);
            // }
            //要记录此人成年的日期，做判断
            // return "success";
            return ret['respCode'];
        } else {
            return ret['respCode'];
        }
    });
}


/**
 * @name setAchievementBall
 * @param string achievementBall 成就球数据,用','分割。
 * 设置成就球
 */

exports.setAchievementBall = async function () {
    const ss = this.get('achievementBall', 'string');
    let achievementBall = ss.split(',');
    for (let i = 0; i < achievementBall.length; i++) {
        this.updater.item.key(parseInt(achievementBall[i]));
    }
    await this.updater.data();
    for (let i = 0; i < achievementBall.length; i++) {
        let data = this.updater.item.get(parseInt(achievementBall[i]));
        if (!data) {
            return this.quick_error("M2saghge20", "成就球不存在", achievementBall[i]);
        }
        //achievementBall 表里验证
        let dataConfig = this.config('achievementBall', data.id);
        if (!dataConfig) {
            return this.quick_error("M2saghge50", "配置不存在", data.id);
        }
    }
    this.updater.role.set('achievementBall', achievementBall);
    return this.updater.save();
};


/**
 * @name setreputation
 * @param string reputation 星光体绰号,用','分割。
 * 设置星光体绰号
 */

exports.setreputation = async function () {
    const ss = this.get('reputation', 'string');
    let reputation = ss.split(',');
    for (let i = 0; i < reputation.length; i++) {
        this.updater.item.key(parseInt(reputation[i]));
    }
    await this.updater.data();
    for (let i = 0; i < reputation.length; i++) {
        let data = this.updater.item.get(parseInt(reputation[i]));
        if (!data) {
            return this.quick_error("M2saghge20", "绰号不存在", reputation[i]);
        }
        //nickName
        let dataConfig = this.config('nickName', data.id);
        if (!dataConfig) {
            return this.quick_error("M2saghge50", "配置不存在", data.id);
        }
    }
    this.updater.role.set('reputation', reputation);
    return this.updater.save();
};


/**
 * @name setAstralImage
 * @param string image 星光体形象,用','分割
 * 设置星光体装扮
 */

exports.setAstralImage = async function () {
    const imageData = this.get('image', 'string');
    let imageArr = imageData.split(',');
    for (let i = 0; i < imageArr.length; i++) {
        if (!imageArr[i]) {
            continue;
        }
        this.updater.item.key(parseInt(imageArr[i]));
    }
    await this.updater.data();
    for (let i = 0; i < imageArr.length; i++) {
        if (!imageArr[i]) {
            continue;
        }
        let imageData = this.updater.item.get(parseInt(imageArr[i]));
        if (!imageData) {
            return this.quick_error("M2saghge20", "装扮不存在", imageArr[i]);
        }
        let dataConfig = this.config('playerCloth', imageData.id);
        if (!dataConfig) {
            return this.quick_error("M2saghge50", "装扮配置不存在", imageData.id);
        }
    }
    this.updater.role.set('astralImage', imageArr);
    return this.updater.save();
};

/**
 * @name setIcon
 * @param string iconID  头像ID
 * 设置玩家头像
 */

exports.setIcon = async function () {
    const id = this.get('iconID', 'string');
    this.updater.item.key(parseInt(id));
    await this.updater.data();
    let itemData = this.updater.item.get(parseInt(id));
    if (!itemData) {
        return this.quick_error('M2saghge190', '物品不存在', id);
    }
    //要去头像表里验证 head
    let dataConfig = this.config('head', itemData.id);
    if (!dataConfig) {
        return this.quick_error("M2saghge50", "配置不存在", itemData.id);
    }
    this.updater.role.set('icon', itemData.id);
    return this.updater.save();
};


/**
 * @name setIconFrame
 * @param string iconFrameID  头像框ID
 * 设置玩家头像框
 */

exports.setIconFrame = async function () {
    const id = this.get('iconFrameID', 'string');
    this.updater.item.key(parseInt(id));
    await this.updater.data();
    let itemData = this.updater.item.get(parseInt(id));
    if (!itemData) {
        return this.quick_error('M2saghge190', '物品不存在', id);
    }
    //要去头像框表里验证 headFrame
    let dataConfig = this.config('headFrame', itemData.id);
    if (!dataConfig) {
        return this.quick_error("M2saghge50", "配置不存在", itemData.id);
    }
    this.updater.role.set('iconFrame', itemData.id);
    return this.updater.save();
};

/**
 * @name businessCard
 * @param string businessCardID  名片ID
 * 设置玩家名片
 */
exports.businessCard = async function () {
    const id = this.get('businessCardID', 'string');
    this.updater.item.key(parseInt(id));
    await this.updater.data();
    let itemData = this.updater.item.get(parseInt(id));
    if (!itemData) {
        return this.quick_error('M2saghge190', '物品不存在', id);
    }
    //要去名片框表里验证 businessCardID
    let dataConfig = this.config('businessCard', itemData.id);
    if (!dataConfig) {
        return this.quick_error("M2saghge50", "配置不存在", itemData.id);
    }
    this.updater.role.set('businessCardID', itemData.id);
    return this.updater.save();
};



/**
 * @name setStarSkill
 * @param int skillID  星光体技能ID
 * 设置玩家星光体技能
 */
exports.setStarSkill = async function () {
    const id = this.get('skillID', 'int');
    // this.updater.item.key(parseInt(id));
    this.updater.role.key('starSkill');
    await this.updater.data();
    let skillData = this.updater.role.get('starSkill') || {};
    let now = this.updater.time;
    if ((skillData.lastUseTime + 1000) > now) {
        //还没过CD
        return this.quick_error("M2saghge50", "技能冷却中", skillData.lastUseTime);
    }
    //TODO:需要去天赋树里验证是否有此技能
    skillData.id = id;
    skillData.lastUseTime = 0;
    this.updater.role.set('starSkill', skillData);
    return this.updater.save();
};

/**
 * @name useStarSkill
 * @param int skillID  星光体技能ID
 * 使用玩家星光体技能
 */
exports.useStarSkill = async function () {
    const id = this.get('skillID', 'int');
    this.updater.role.key('starSkill');
    await this.updater.data();
    let skillData = this.updater.role.get('starSkill') || {};
    let now = this.updater.time;
    if (skillData.id != id) {
        return this.quick_error("M2saghge50", "无法使用本技能", skillData.id);
    }
    if ((skillData.lastUseTime + 1000) > now) {
        //还没过CD
        return this.quick_error("M2saghge50", "技能冷却中", skillData.lastUseTime);
    }
    //TODO:需要去天赋树里验证是否有此技能
    skillData.id = id;
    skillData.lastUseTime = this.updater.time;
    this.updater.role.set('starSkill', skillData);
    return this.updater.save();
};


/**
 * @name setHex
 * @param array data 完全数据
 * 设置海克斯科技
 */

exports.setHex = async function () {
    const ss = this.get('data', 'array');
    if (!ss) {
        return this.quick_error('M2saghge140', ' 参数错误');
    }
    //TODO:以后表里验证数据
    this.updater.role.set('hex', ss);
    return this.updater.save();
};

/**
 * @name setSpaceProgress
 * @param array data 完全数据
 * 空间进度设置数据
 */

exports.setSpaceProgress = async function () {
    const ss = this.get('data', 'array');
    if (!ss) {
        return this.quick_error('M2saghge140', ' 参数错误');
    }
    //TODO:以后表里验证数据
    this.updater.role.set('spaceProgress', ss);
    return this.updater.save();
};



/**
 * @name setblock
 * @param array data 完全数据
 * 设置条状的app
 */

exports.setblock = async function () {
    const ss = this.get('data', 'array');
    if (!ss) {
        return this.quick_error('M2saghge140', ' 参数错误');
    }
    //TODO:以后表里验证数据
    this.updater.role.set('block', ss);
    return this.updater.save();
};




/**
 * @name setAppUnit
 * @param string key  设置key
 * @param string data  设置内容
 * 设置玩家app助理
 */

exports.setAppUnit = async function () {
    //TODO:缺乏验证
    const key = this.get('key', 'string');
    const data = this.get('data', 'string');
    // this.updater.item.key(parseInt(id));
    await this.updater.data();
    // let itemData = this.updater.item.get(parseInt(id));
    // if (!itemData) {
    //     return this.quick_error('M2saghge190', '物品不存在', id);
    // }
    switch (key) {
        case "backgroud":
            this.updater.role.set('appUnit.' + key, data);
            break;
        case "unit01":
            this.updater.role.set('appUnit.' + key, data);
            break;
        case "unit02":
            this.updater.role.set('appUnit.' + key, data);
            break;
        default:
            return this.quick_error("M2saghge50", "配置不存在", key);
    }
    //要去头像框表里验证 headFrame
    // let dataConfig = this.config('headFrame',itemData.id);
    // if (!dataConfig) {
    //     return this.quick_error("M2saghge50", "配置不存在", itemData.id);
    // }
    // this.updater.role.set('iconFrame',itemData.id);
    return this.updater.save();
};


/**
 * @name setpackage
 * @param int data 成就包装袋ID
 * 设置成就包装袋
 */
exports.setpackage = async function () {
    const pagedata = this.get('data', 'int');
    if (!pagedata) {
        return this.quick_error('M2saghge140', '参数错误');
    }
    //验证成就包表 achievementPacket
    let dataConfig = this.config('achievementPacket', pagedata);
    if (!dataConfig) {
        return this.quick_error('M2saghge50', "配置不存在", pagedata);
    }
    this.updater.item.key(parseInt(pagedata));
    await this.updater.data();
    let itemData = this.updater.item.get(parseInt(pagedata));
    if (!itemData) {
        return this.quick_error('M2saghge190', '物品不存在', pagedata);
    }
    this.updater.role.set('package', pagedata);
    return this.updater.save();
}

/**
 * @name setSpaceUnit
 * @param int data 个人空间助理ID
 * 设置个人空间助理
 */
exports.setSpaceUnit = async function () {
    const spaceUnitData = this.get('data', 'int');

    //判断获取数据是否为空
    if (!spaceUnitData) {
        return this.quick_error('M2saghge140', '参数错误');
    }
    this.updater.role.set('spaceUnit', spaceUnitData);
    return this.updater.save();
}

